var ck = {
    setBtnStatus:function () {
        var row = $('#grid_ck').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_ck_mod").show();
            $("#btn_ck_del").show();
        } else {
            $("#btn_ck_mod").hide();
            $("#btn_ck_del").hide();
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_ck').datagrid({
        url: $.webPath() + "/organ/ck/ckList",
        toolbar: "#toolbar_ck",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        pageNumber: 1,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'bh', title: '编号', width: 25},
                {field: 'mc', title: '名称', width: 25},
                {field: 'ssgsjg_jc', title: '所属公司', width: 30},
                {field: 'lxr', title: '联系人', width: 15},
                {field: "dz", title: "地址", width: 40} ,
                {field: "ckjb", title: "仓库级别", width: 10,
                    formatter: function (value) {
                        if (value == '1') {
                            return '经营仓库';
                        } else if (value == '2') {
                            return '保供仓库';
                        } else if (value == '3') {
                            return '三方仓库';
                        }
                    }
                }
            ]
        ],
        onLoadSuccess:ck.setBtnStatus,
        onClickRow:ck.setBtnStatus
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_ck_add").click(function () {
        ck_add('新增仓库', 'add_ck');
    });
    //修改按钮
    $("#btn_ck_mod").click(function () {
        ck_modify('修改仓库', 'update_ck');
    });
    //删除按钮
    $("#btn_ck_del").click(function () {
        ck_delete();
    });
    //查询按钮
    $("#btn_ck_search").click(function () {
        ck_search();
    });
    //双击事件
    $("#grid_ck").datagrid({
        onDblClickCell:function (index, field) {
            if (field == "bh") {
                ck_modify('修改仓库', 'update_ck');
            }
        }
    });
    //加载查询条件中的所属公司下拉框
    $("#ck_ssgsjg").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//新增仓库
function ck_add(title, action) {
    var href = $.webPath() + "/organ/ck/ck_content";
    var actionUrl = $.webPath() + "/organ/ck/" + action;
    ck_showDialog(title, href, actionUrl);
}

//修改仓库
function ck_modify(title, action) {
    var record = $("#grid_ck").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/organ/ck/ck_content?ckId=" + record.id;
    var actionUrl = $.webPath() + "/organ/ck/" + action;
    ck_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function ck_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:580,
        height:300,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    //$.messager.progress();
                    $(win).mask();
                    $('#form_ck').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                //$.messager.progress('close');
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_ck').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除仓库
function ck_delete() {
    var row = $('#grid_ck').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该仓库？', function (r) {
            if (r) {
                var index = $('#grid_ck').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/organ/ck/delete_ck",
                    dataType: 'json',
                    type: 'POST',
                    data: {ckId:row.id},
                    beforeSend: function () {
                        $("#layout_ck").mask();
                    },
                    success:function(data){
                        $("#layout_ck").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_ck').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
//                $.post($.webPath() + '/organ/ck/delete_ck', {ckId:row.id}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.statusCode == '200') {
//                        $('#grid_ck').datagrid('deleteRow', index);
//                        $.messager.show({
//                            title:'提示',
//                            msg:obj.message,
//                            timeout:3000,
//                            showType:'slide'
//                        });
//                    } else {
//                        $.messager.alert("提示", obj.message, "error");
//                    }
//                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的仓库！", "warning");
    }
}

//查询仓库
function ck_search() {
    var ssgsjg = $('#ck_ssgsjg').combotree('getText') == null ? null : $('#ck_ssgsjg').combotree('getText');
    var ssgsjgId = $('#ck_ssgsjg').combotree('getValue') == null ? null : $('#ck_ssgsjg').combotree('getValue');
  //  alert($('#query_ck_mc').val());
    $('#grid_ck').datagrid('load',
        {
            mc:$('#query_ck_mc').val(),
            ssgsjg:ssgsjg,
            ssgsjgId:ssgsjgId
        }
    );
}
